package com.atguigu.interview.test;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author Jungle
 * @create 2022-09-14 22:43
 */
public class LRUCacheDemo<K,V> extends LinkedHashMap<K,V>{
    private int capacity;


    /**
     * 1、 the ordering mode - <tt>true</tt> for  access-order,
     * <tt>false</tt> for insertion-order
     *
     * [1, 2, 3]
     * [2, 3, 4]
     * [2, 4, 3]
     * [2, 4, 3]
     */
    public LRUCacheDemo( int capacity) {
        super(capacity, 0.75f,true);
        this.capacity = capacity;
    }

    @Override
    protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
        return super.size() > capacity;
    }





    public static void main(String[] args) {
        LRUCacheDemo<Object, Object> lruCacheDemo = new LRUCacheDemo<>(3);
        lruCacheDemo.put(1, "a");
        lruCacheDemo.put(2, "b");
        lruCacheDemo.put(3, "c");
        System.out.println(lruCacheDemo.keySet());
        lruCacheDemo.put(4, "d");
        System.out.println(lruCacheDemo.keySet());
        lruCacheDemo.put(3, "c");
        System.out.println(lruCacheDemo.keySet());
        lruCacheDemo.put(3, "c");
        System.out.println(lruCacheDemo.keySet());
        lruCacheDemo.put(5, "c");
        System.out.println(lruCacheDemo.keySet());
        lruCacheDemo.put(3, "x");
        System.out.println(lruCacheDemo.keySet());
    }
}
